<?php

namespace App\Http\Controllers\Manage;

use App\Client;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class ClientController extends Controller
{
    /**
     * @var int 数据表id
     */
    public $table_id = 7;

    /**
     * CaseController constructor.
     */
    public function __construct()
    {
        parent::__construct($this->table_id);

    }
    /**
     * 印刷供应商列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index() {
        #判断权限
        if($this->rule['view'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        $search = Input::get('search');
        if($search){
            #获取搜索内容
            $client = Client::where('name','like',"%$search%")
                ->orderBy('rank','desc')
                ->orderBy('update_time','desc')
                ->paginate(10);
        }else{
            #获取全部列表信息
            $client = Client::orderBy('rank','desc')
                ->orderBy('update_time','desc')
                ->paginate(10);
        }

        return view('manage.client.index',['client'=>$client]);
    }
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add()
    {
        #判断权限

        if($this->rule['add'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        return view('manage.client.create');
    }
    /**
     * 增加印刷供应商
     * @return array
     */
    public function addClient()
    {
        $rule = [
            'name' => 'required',
            'phone' => 'required',
            'linkman' => 'required',
            'address' => 'required',
            'rank' => 'required',
        ];
        $message = [
            'required' => ':attribute 不能为空'
        ];
        $attributes = [
            'name' => '公司名称',
            'linkman' => '联系人',
            'phone' => '联系方式',
            'address' => '详细地址',
            'rank' => '权重'
        ];
        $validator = Validator::make(Input::all(), $rule, $message, $attributes);
        if($validator->fails()){
            $warnings = $validator->messages();
            $show_warning = $warnings->first();
            return [
                'error'=>2,
                'msg'=>$show_warning,
            ];
        }
        $client = new Client();
        $client->name = Input::get('name');//公司名称
        $client->linkman = Input::get('linkman');//联系人
        $client->phone = Input::get('phone');//联系方式
        $client->address = Input::get('address');//详细地址
        $client->province = Input::get('province');//省
        $client->city = Input::get('city');//市
        $client->county = Input::get('county');//区
        $client->scope = Input::get('scope');//经营范围
        $client->remark = Input::get('remark');//备注
        if($client->save()){
            return [
                'error'=>0,
                'msg'=>'添加成功！'
            ];
        }else{
            return [
                'error'=>1,
                'msg'=>'添加失败！'
            ];
        }
    }
    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        #判断权限
        if($this->rule['update'] == 0){
            return redirect()->back()->withInput()->withErrors('权限不足！');
        }
        #获取对应id的客户信息
        $clientInfo = Client::where('id',$id)->first();
        return view('manage.client.edit',['clientInfo'=>$clientInfo]);
    }
    /**
     * 保存修改信息
     * @return array
     */
    public function save()
    {
        $rule = [
            'name' => 'required',
            'phone' => 'required',
            'linkman' => 'required',
            'address' => 'required',
            'rank' => 'required',
        ];
        $message = [
            'required' => ':attribute 不能为空'
        ];
        $attributes = [
            'name' => '公司名称',
            'linkman' => '联系人',
            'phone' => '联系方式',
            'address' => '详细地址',
            'rank' => '权重'
        ];
        $validator = Validator::make(Input::all(), $rule, $message, $attributes);
        if($validator->fails()){
            $warnings = $validator->messages();
            $show_warning = $warnings->first();
            return [
                'error'=>2,
                'msg'=>$show_warning,
            ];
        }
        $client = Client::find(Input::get('id'));
        $client->name = Input::get('name');//公司名称
        $client->linkman = Input::get('linkman');//联系人
        $client->phone = Input::get('phone');//联系方式
        $client->address = Input::get('address');//详细地址
        $client->province = Input::get('province');//省
        $client->city = Input::get('city');//市
        $client->county = Input::get('county');//区
        $client->scope = Input::get('scope');//经营范围
        $client->remark = Input::get('remark');//备注
        if($client->save()){
            return [
                'error'=>0,
                'msg'=>'保存成功！'
            ];
        }else{
            return [
                'error'=>1,
                'msg'=>'保存失败！'
            ];
        }
    }
    /**
     * //禁用该分类
     * @return array
     */
    public function setStatus() {
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 0) {
            $del = Client::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }else{
            $del = Client::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！',
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }
}
